//Tranz-Am
#include "game_transam.h"

static std::string ModesAvailable[]={"Double Buffering","System Double Buffering","Paging","Triple Buffering"};

bool TransAmMenuLogic()
{
	//just to show its in a timer context, we'll draw the animation cup
	Cup->NextMove();
	
	//see what options have been selected
	//if 'autowritemain' is set to true in the config.xml file then
	//all these changes will be kept and loaded automatically
	bool restart;
	if(keypressed())
	{
		restart=true;
		switch(readkey()>>8)
		{
			case KEY_0:
				return true;
				break;
			case KEY_X:
				Lives=3;
				TempLocalRadar=1;
				restart=false;
				GameFramework->ChangeTimerType(GAME_GAME);
				break;
			case KEY_Y:
				Lives=3;
				TempLocalRadar=2;
				restart=false;
				GameFramework->ChangeTimerType(GAME_GAME);
				break;
			case KEY_2:
				GameConfiguration->CapsGraphics.WindowMode="windowed";
				break;
			case KEY_3:
				GameConfiguration->CapsGraphics.WindowMode="fullscreen";
				break;
			case KEY_4:
				if(GameConfiguration->CapsActualSystem.GraphicsMode!=MODE_DOUBLE)
					GameConfiguration->CapsGraphics.GraphicsMode=MODE_DOUBLE;
				else
					restart=false;
				break;
			case KEY_5:
				if(GameConfiguration->CapsActualSystem.GraphicsMode!=MODE_SYSTEMDOUBLE)
					GameConfiguration->CapsGraphics.GraphicsMode=MODE_SYSTEMDOUBLE;
				else
					restart=false;
				break;
			case KEY_6:
				if(GameConfiguration->CapsActualSystem.GraphicsMode!=MODE_PAGED)
					GameConfiguration->CapsGraphics.GraphicsMode=MODE_PAGED;
				else
					restart=false;
				break;
			case KEY_7:
				if(GameConfiguration->CapsActualSystem.GraphicsMode!=MODE_TRIPLE)
					GameConfiguration->CapsGraphics.GraphicsMode=MODE_TRIPLE;
				else
					restart=false;
				break;
			case KEY_8:
				if(GameConfiguration->CapsGraphics.VSync==false)
					GameConfiguration->CapsGraphics.VSync=true;
				else
					restart=false;
				break;
			case KEY_9:
				if(GameConfiguration->CapsGraphics.VSync==true)
					GameConfiguration->CapsGraphics.VSync=false;
				else
					restart=false;
				break;
			case KEY_A:
				if(GameConfiguration->CapsSystem.MatchRefreshRate==false)
					GameConfiguration->CapsSystem.MatchRefreshRate=true;
				else
					restart=false;
				break;
			case KEY_B:
				if(GameConfiguration->CapsSystem.MatchRefreshRate==true)
					GameConfiguration->CapsSystem.MatchRefreshRate=false;
				else
					restart=false;
				break;
			case KEY_C:
				GameConfiguration->CapsGraphics.ScrWidth=640;
				GameConfiguration->CapsGraphics.ScrHeight=480;
				break;
			case KEY_D:
				GameConfiguration->CapsGraphics.ScrWidth=800;
				GameConfiguration->CapsGraphics.ScrHeight=600;
				break;
			default:
				restart=false;
		}

		//a change has happened that requires a graphics restart
		//need to redo animations and stuff too
		if(restart)
		{
			EndGame();
			if(!GameFramework->RestartSystem())
			{
				Configuration::GlobalErrorString="Failed to restart. Amend config.xml before trying to start";
				return true;
			}
			else
			{
				SetupGame();
			}
		}
	}


	return false;
}

bool TransAmMenuDrawing()
{
	int start=1;
	
	clear(GameFramework->DrawingSurface);
	//the spinning cup
	draw_sprite(GameFramework->DrawingSurface, Cup->ReadOnly_CurrentItem, 280, 130);
	
	//menu options
	textprintf_ex(GameFramework->DrawingSurface,font,0,0,makecol(255,255,255),0,"FPS %d",TimerCurrentFPS);
	textout_ex(GameFramework->DrawingSurface, font,
				"X Play Standard Radar, Y Play Reverse Radar",0,(start++)*10,makecol(255,255,255),0);
	textout_ex(GameFramework->DrawingSurface, font,
				"2 Windowed",0,(start++)*10,makecol(255,255,255),0);
	textout_ex(GameFramework->DrawingSurface, font,
				"3 Fullscreen",0,(start++)*10,makecol(255,255,255),0);
	textout_ex(GameFramework->DrawingSurface, font,
				"4 Double Buffered",0,(start++)*10,makecol(255,255,255),0);
	textout_ex(GameFramework->DrawingSurface, font,
				"5 System Double Buffering",0,(start++)*10,makecol(255,255,255),0);
	textout_ex(GameFramework->DrawingSurface, font,
				"6 Paging",0,(start++)*10,makecol(255,255,255),0);
	textout_ex(GameFramework->DrawingSurface, font,
				"7 Triple Buffering",0,(start++)*10,makecol(255,255,255),0);
	textout_ex(GameFramework->DrawingSurface, font,
				"8 Vsync On (double buffering only)",0,(start++)*10,makecol(255,255,255),0);
	textout_ex(GameFramework->DrawingSurface, font,
				"9 Vsync Off",0,(start++)*10,makecol(255,255,255),0);
	textout_ex(GameFramework->DrawingSurface, font,
				"A Try to change Refresh rate to match FPS (60)",0,(start++)*10,makecol(255,255,255),0);
	textout_ex(GameFramework->DrawingSurface, font,
				"B Do not try to change Refresh ",0,(start++)*10,makecol(255,255,255),0);
	textout_ex(GameFramework->DrawingSurface, font,
				"C 640x480 resolution",0,(start++)*10,makecol(255,255,255),0);
	textout_ex(GameFramework->DrawingSurface, font,
				"D 800x600 resolution",0,(start++)*10,makecol(255,255,255),0);
	textout_ex(GameFramework->DrawingSurface, font,
				"0 Exit",0,(start++)*10,makecol(255,255,255),0);

	//now list what we have
	start+=3;
	if(GameConfiguration->CapsGraphics.WindowMode=="windowed")
		textout_ex(GameFramework->DrawingSurface, font,
				"Windowed mode",0,(start++)*10,makecol(255,0,0),0);
	else
		textout_ex(GameFramework->DrawingSurface, font,
				"Fullscreen mode",0,(start++)*10,makecol(255,0,0),0);

	textprintf_ex(GameFramework->DrawingSurface,font,0,(start++)*10,makecol(255,0,0),0,
		"%d x %d resolution",GameConfiguration->CapsGraphics.ScrWidth,GameConfiguration->CapsGraphics.ScrHeight);

	textprintf_ex(GameFramework->DrawingSurface,font,0,(start++)*10,makecol(255,0,0),0,
		"%s buffers used",ModesAvailable[GameConfiguration->CapsActualSystem.GraphicsMode].c_str());

	if(GameConfiguration->CapsGraphics.VSync)
		textout_ex(GameFramework->DrawingSurface, font,
				"VSync enabled",0,(start++)*10,makecol(255,0,0),0);
	else
		textout_ex(GameFramework->DrawingSurface, font,
				"VSync disabled",0,(start++)*10,makecol(255,0,0),0);

	if(GameConfiguration->CapsSystem.MatchRefreshRate)
		textout_ex(GameFramework->DrawingSurface, font,
				"Alter refresh rate enabled (though may not have worked)",0,(start++)*10,makecol(255,0,0),0);
	else
		textout_ex(GameFramework->DrawingSurface, font,
				"Alter refresh rate disabled",0,(start++)*10,makecol(255,0,0),0);
		return false;
}

